<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" showFooter :title="getTitle" width="70%" @ok="handleSubmit">
    <BasicForm @register="registerForm">

    </BasicForm>

  </BasicDrawer>
</template>
<script lang="ts" setup>
import {ref, computed, unref, reactive} from 'vue';
import { BasicForm, useForm, ApiSelect } from '@/components/Form';
import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
import {ruleSchema, teamUserColumns,ruleTableSchema} from './role.data';
import { useModal } from "@/components/Modal";
import { teamSave,teamUserList } from '@/api/heng/ex';
import { useMessage } from "@/hooks/web/useMessage";
import { useUserStore } from "@/store/modules/user";
import {TableAction} from "@/components/Table";
import {Button} from "ant-design-vue";
import {BasicTableProps} from "@/components/VxeTable";
const userStore = useUserStore();

const [registerModal, { openModal }] = useModal();

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);

const { createMessage } = useMessage();

const id = ref("");
const [registerForm, { resetFields, setFieldsValue, validate, clearValidate }] = useForm({
  labelWidth: 100,
  baseColProps: { lg: 12, md: 24 },
  schemas: ruleSchema,
  showActionButtonGroup: false,
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  //打开弹层后的回调
  resetFields();
  setDrawerProps({ confirmLoading: false });
  // 需要在setFieldsValue之前先填充treeData，否则Tree组件可能会报key not exist警告
  // if (unref(treeData).length === 0) {
  //   treeData.value = (await getMenuList()) as any as TreeItem[];
  // }
  isUpdate.value = !!data?.isUpdate;
  id.value = "";
  if (unref(isUpdate)) {
    id.value = data.record.id;
    setFieldsValue(data.record);
  } else {
    //新增

  }
});


const tableRef = ref();
const addNum=ref(0)

const getTitle = computed(() => (!unref(isUpdate) ? '新增规则' : '编辑规则'));

async function handleSubmit() {
  try {
    const values = await validate();

    setDrawerProps({ confirmLoading: true });
    // TODO custom api
    // values.teamId = id.value;
    // createFn(values);
    emit("success",values);
    closeDrawer();
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}

function createFn(values) {
  teamSave(values).then((res) => {
    // console.log("---res::",res)
    if (res.code == 200) {
      createMessage.success(res.message);
    } else {
      createMessage.error(res.message);
    }
  })
    .catch((res) => {
      createMessage.error(res.message);
    })
    .finally(() => {
      closeDrawer();
      emit('success');
    });
}
function getActions(record) {
  let arr = [
    {
      label: '删除',
      color: 'error',
      onClick: handleClose.bind(null, record)
    }
  ];


  return arr;
}

function handleClose(record: Recordable) {

  tableRef.value.remove(record);

}
async function addFn() {
  openModal(true, {  })

}

function handleSuccess(list) {
  // console.log("--list:",list)

}


</script>
